package main

import (
	"fmt"
	"github.com/garyburd/redigo/redis"  //引入redis包
)

func main()  {
	
	//连接到redis
	conn, err := redis.Dial("tcp","127.0.0.1:6379")

	if err != nil {
		fmt.Println("redis连接失败,err=",err)
		return
	}

	fmt.Println("redis连接成功,conn=",conn)

	defer conn.Close()

	//通过go向redis写入数据string[key-value]
	_, err = conn.Do("HSet","user","name","test陈")
	if err != nil {
		fmt.Println("redis写入错误,err=",err)
		return
	}
	fmt.Println("写入成功")

	_, err = conn.Do("HSet","user","age",20)
	if err != nil {
		fmt.Println("redis写入错误,err=",err)
		return
	}
	fmt.Println("写入成功")

	content1, err := redis.String(conn.Do("HGet","user","name"))
	if err != nil {
		fmt.Println("redis读取出错,err=",err)
		return
	}

	content2, err := redis.Int(conn.Do("HGet","user","age"))
	if err != nil {
		fmt.Println("redis读取出错,err=",err)
		return
	}
	fmt.Printf("redis读取结果 name=%v, age=%v\n",content1,content2)
}


